#include "minPathSum.h"
int minPathSum(std::vector<std::vector<int>> &grid) {
    int rows = grid.size();
    int cols = grid[0].size();
    int dp[rows][cols];
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            if (r == 0 && c == 0)
                dp[r][c] = grid[0][0];
            else if (r == 0 && c != 0)
                dp[r][c] = dp[r][c - 1] + grid[r][c];
            else if (c == 0 && r != 0)
                dp[r][c] = dp[r - 1][c] + grid[r][c];
            else {
                dp[r][c] = dp[r - 1][c] < dp[r][c - 1]
                               ? dp[r - 1][c] + grid[r][c]
                               : dp[r][c - 1] + grid[r][c];
            }
        }
    }
    return dp[rows - 1][cols - 1];
}
